package me.minecraft.plugin.filemonitor;

import lombok.Getter;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public final class LGFileMonitor extends JavaPlugin {

    @Getter
    private static LGFileMonitor instance;

    @Getter
    private ObserverThread observer;

    @Override
    public void onEnable() {
        instance = this;
        this.saveDefaultConfig();
        this.loadFileListeners();
    }

    @Override
    public void onDisable() {
        this.observer.stop();
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (args.length == 0) {
            sender.sendMessage("§6[文件监视器]§f 作者: langle__");
            sender.sendMessage("§6[文件监视器]§f 版本: " + this.getDescription().getVersion());
            sender.sendMessage("§6[文件监视器]§f 用途: /fm reload");
            return true;
        }
        String cmd = args[0].toLowerCase();
        if (cmd.equals("reload")) {
            this.reloadConfig();
            this.loadFileListeners();
            sender.sendMessage("§6[文件监视器]§f 配置重载完成");
        }
        return true;
    }

    @Override
    public @NotNull List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
        return Collections.singletonList("reload");
    }

    private void loadFileListeners() {
        if (this.observer != null) this.observer.cancel();
        this.observer = new ObserverThread(this);
        FileConfiguration config = this.getConfig();
        for (String key : config.getKeys(false)) {
            ConfigurationSection section = config.getConfigurationSection(key);
            if (section == null) continue;
            String path = section.getString("path");
            String command = section.getString("command");
            if (path == null || command == null) {
                this.getLogger().warning("文件监听器配置错误: " + key);
                continue;
            }
            File file = new File(path);
            if (!file.exists()) {
                this.getLogger().warning("文件不存在: " + path);
                continue;
            }
            if (file.isDirectory()) {
                for (File f : listAllFiles(file)) {
                    observer.addFileListener(new FileListener(f, command));
                }
            } else if (file.isFile()) {
                observer.addFileListener(new FileListener(file, command));
            }
        }
        this.observer.start();
    }

    public static List<File> listAllFiles(File directory) {
        if (!directory.exists()) return Collections.emptyList();
        if (!directory.isDirectory()) return Collections.singletonList(directory);
        File[] files = directory.listFiles();
        if (files == null) return Collections.emptyList();
        List<File> result = new ArrayList<>();
        for (File file : files) {
            if (file.isDirectory()) {
                result.addAll(listAllFiles(file));
            } else {
                result.add(file);
            }
        }
        return result;
    }

}
